package huixue.cn.com.rxapp.matrix;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.base.SimpleActivity;

public class MatrixActivity extends SimpleActivity {

    @BindView(R.id.btn_scale)
    Button btnScale;

    Paint mPaint;
    @BindView(R.id.iv_base)
    ImageView ivBase;
    @BindView(R.id.iv_after)
    ImageView ivAfter;
    Bitmap mBitmap;
    @BindView(R.id.btn_ration)
    Button btnRation;
    @BindView(R.id.btn_translate)
    Button btnTranslate;

    @Override
    public int getLayout() {
        return R.layout.activity_matrix;
    }

    @Override
    public void initView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    public void addEvent() {
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        ivBase.setImageBitmap(mBitmap);
    }


    @OnClick({R.id.btn_scale,R.id.btn_ration, R.id.btn_translate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_scale:
                scaleBitmap(2);
                break;
            case R.id.btn_ration:
                RotateBitmap(120);
                break;
            case R.id.btn_translate:
                break;

        }

    }

    private void scaleBitmap(float scale) {
        //创建一个bitmap
        Bitmap afterBitmap = Bitmap.createBitmap((int) (mBitmap.getWidth() * scale), (int) (mBitmap.getHeight() * scale), mBitmap.getConfig());

        // 用bitmap 创建画布
        Canvas canvas = new Canvas(afterBitmap);

        Matrix matrix = new Matrix();

        matrix.setScale(scale, scale);

        // 根据缩放比例，把图片draw到Canvas上
        canvas.drawBitmap(mBitmap, matrix, mPaint);

        ivAfter.setImageBitmap(afterBitmap);
    }


    private void RotateBitmap(float rotate){
        //创建一个bitmap
        Bitmap afterBitmap = Bitmap.createBitmap(mBitmap.getWidth()+100, mBitmap.getHeight()+100, mBitmap.getConfig());

        // 用bitmap 创建画布
        Canvas canvas = new Canvas(afterBitmap);

        Matrix matrix = new Matrix();
        matrix.postRotate(rotate,mBitmap.getWidth() / 2,
                mBitmap.getHeight() / 2);

        matrix.postTranslate(100,100);
        canvas.drawBitmap(mBitmap, matrix, mPaint);
        ivAfter.setImageBitmap(afterBitmap);
    }


}
